package com.climber.android.lib_share.sensor

import android.os.Handler
import android.os.Looper
import android.util.Log
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import com.climber.android.lib_share.OneShare
import java.util.HashMap

/**
 * Created by Mikes at 2020-03-20 09:35
 */
class MyShareActionListener(private val builderTT: ShareResultCallBack.Builder.() -> Unit) : PlatformActionListener{
    private val mainHandler : Handler = Handler(Looper.getMainLooper())
    private var shareResultCallBack : ShareResultCallBack?=null
    init {
        shareResultCallBack = ShareResultCallBack().apply { registerListener(builderTT) }
    }

    override fun onComplete(platform: Platform, p1: Int, value: HashMap<String, Any>?) {
        Log.d(OneShare.TAG, "${platform.name}  share success")

        mainHandler.post {
            shareResultCallBack?.builder?.success?.invoke()
        }
    }

    override fun onCancel(platform: Platform, code: Int) {
        Log.d(OneShare.TAG, "${platform.name}  share cancel, cancelCode : $code ")

        mainHandler.post {
            shareResultCallBack?.builder?.cancel?.invoke()
        }
    }

    override fun onError(platform: Platform, code: Int, throwable: Throwable?) {
        Log.d(OneShare.TAG, "${platform.name} share error, errorCode : $code, cause by ${Log.getStackTraceString(throwable)}")
        mainHandler.post {
            shareResultCallBack?.builder?.error?.invoke()
        }
    }
}